package com.lixueju.security.box.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixueju.security.box.core.enums.TableFieldEnum;
import com.lixueju.security.box.core.model.AuthRole;
import com.lixueju.security.box.core.model.request.UpdatePermissionRequest;
import com.lixueju.security.box.web.service.IAuthRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 储存角色信息 前端控制器
 * </p>
 *
 * @author lixueju
 * @since 2019-11-13
 */
@RestController
@RequestMapping(value = "/security/box/authRole", produces = "application/json;charset=utf-8")
public class AuthRoleController extends SecurityBoxBaseController {
    @Autowired
    private IAuthRoleService authRoleService;

    @GetMapping("/authRoleList")
    public String authRoleList(AuthRole authRole) {
        Page<AuthRole> page = getPage(true);
        return callbackSuccess(authRoleService.selectAuthRolePage(page, authRole));
    }

    @PostMapping("/addOrUpdateAuthRole")
    public String addOrUpdateAuthRole(@RequestBody AuthRole authRole) {
        return callbackSuccess(authRoleService.saveOrUpdate(authRole));
    }

    /**
     * 修改角色权限
     */
    @PostMapping("/updatePermission")
    public String updatePermission(@RequestBody UpdatePermissionRequest updatePermissionRequest) {
        return callbackSuccess(authRoleService.updatePermission(updatePermissionRequest));
    }

    @PostMapping("/deleteAuthRole")
    public String deleteAuthRole(@RequestBody AuthRole authRole) {
        return callbackSuccess(authRoleService.deleteAuthRole(authRole));
    }

    /**
     * 获取所有角色（给用户赋值角色时需要）
     */
    @GetMapping("/getAllAuthRoleForUser")
    public String getAllAuthRoleForUser() {
        QueryWrapper<AuthRole> quAuthRole = new QueryWrapper<>();
        quAuthRole.eq("delFlag", TableFieldEnum.AllOfdelFlag.no.code());
        quAuthRole.select("id", "name");
        return callbackSuccess(authRoleService.list(quAuthRole));
    }
}

